package hlhj.fhp.supreme.activitys

import android.annotation.SuppressLint
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.os.Handler
import android.os.Message
import com.lzy.okgo.OkGo
import com.lzy.okgo.model.Response
import hlhj.fhp.supreme.BaseAty
import hlhj.fhp.supreme.R
import hlhj.fhp.supreme.javabean.BaseBean
import hlhj.fhp.supreme.javabean.UserInfoBean
import hlhj.fhp.supreme.network.Urls
import hlhj.fhp.supreme.utils.JsonCallBack
import hlhj.fhp.supreme.utils.MyUtils
import hlhj.fhp.supreme.utils.SPHelper
import kotlinx.android.synthetic.main.activity_un_bind_aty.*

class UnBindAty : BaseAty() {
    private var sec=60
    private var etStr="1"
    private lateinit var user:UserInfoBean.DataBean
    private val  handler= @SuppressLint("HandlerLeak")

    object : Handler(){
        override fun handleMessage(msg: Message?) {
            super.handleMessage(msg)
            if (sec>0){
                sec--
                btSms.text="$sec s"
                this.sendEmptyMessageDelayed(1,1000)
            }else{
                sec=60
                btSms.text="验证码"
                btSms.isClickable=true
            }
        }
    }
    override fun getContentId(): Int {
        return R.layout.activity_un_bind_aty
    }

    override fun initView() {

        user=SPHelper.getUser(this) as UserInfoBean.DataBean
    }
    override fun initListener() {

        btExit.setOnClickListener {
            finish()
        }

        btSms.setOnClickListener {
                OkGo.post<BaseBean>(Urls.BaseUrl+Urls.SEND_SMS)
                        .params("mobile",user.mobile)
                        .execute(object : JsonCallBack<BaseBean>(){
                            override fun onSuccess(response: Response<BaseBean>?) {
                                val bean = response?.body()
                                if (bean?.code==1){
                                    MyUtils.toast("发送成功")
                                    btSms.text="$sec s"
                                    btSms.isClickable=false
                                    handler.sendEmptyMessage(1)
                                }
                            }
                        })
            }
        btCommit.setOnClickListener {
            if (etYzm.text.isEmpty()){
                MyUtils.toast("请输入验证码")
            }else{
                OkGo.post<BaseBean>(Urls.BaseUrl+Urls.UNBIND)
                        .params("type",intent.getStringExtra("type"))
                        .params("captcha",etYzm.text.toString())
                        .execute(object :JsonCallBack<BaseBean>(this@UnBindAty){
                            override fun onSuccess(response: Response<BaseBean>?) {
                                val bean = response?.body()
                                if (bean?.code==1){
                                    MyUtils.toast("解除绑定成功")
                                    finish()
                                }
                            }
                        })
            }
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        handler.removeMessages(1)
    }
}
